﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project
{
    public partial class Login : Form
    {
        // fields to save the username and the permissions
        // of the user
        public static string username;
        public static int permissions = 100;
        public static string id;

        public Login()
        {
            InitializeComponent();
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            // creating a datatable that has the data of the
            // username that was given
            DataTable dt=new DataTable();
            DataRow[] dr;
            int duty;
            string password, query;
            query = "SELECT fld_employeeID, fld_password, fld_duty FROM tbl_Employees WHERE fld_userName Like '" + txt_user.Text + "'";
            try
            {
                dt = AdoHelper.ExecuteDataTable(query);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR");
            }
            dr = dt.Select();
            // checking if the username exist, if it is
            // checking if the password is correct, if it
            // we save the duty and according to its value
            // we link the user to a relevant form
            try
            {
                password = dr[0].Field<string>("fld_password");
                id = dr[0].Field<string>("fld_employeeID");
                if (password.Equals(txt_pass.Text))
                {
                    username = txt_user.Text;
                    duty = dr[0].Field<int>("fld_duty");
                    query = "SELECT fld_permissions FROM tbl_duties WHERE fld_dutyID=" + duty;
                    dt = AdoHelper.ExecuteDataTable(query);
                    dr = dt.Select();
                    permissions = dr[0].Field<int>("fld_permissions");
                    switch (permissions)
                    {
                        case 0:
                            CEOForm ceoForm = new CEOForm();
                            ceoForm.Show();
                            break;
                        case 1:
                            SVForm svForm = new SVForm();
                            svForm.Show();
                            break;
                        case 2:
                            DForm dForm = new DForm();
                            dForm.Show();
                            break;
                        case 3:
                            AccountantForm aForm = new AccountantForm();
                            aForm.Show();
                            break;
                        case 4:
                            HRForm hrForm = new HRForm();
                            hrForm.Show();
                            break;
                        default:
                            MessageBox.Show("You don't have permission to the system", "Invalid login");
                            break;
                    }
                }
                else
                    MessageBox.Show("The password you've entered is invalid", "Invalid login");
            }
            catch 
            {
                MessageBox.Show("The username doesn't exist in the system", "Invalid login");
            }
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
